package com.ifudata.dataexpert.shiro;

import java.util.HashSet;
import java.util.Set;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.ifudata.dataexpert.util.JWTUtil;

/**
 * Created with eclipse
 *
 * @Author libing@ifudata.com
 * @Description
 * @Date 2018-09-18
 * @Time 16:56
 */
@Component
public class CustomRealm extends AuthorizingRealm {
	private static final Logger log = LoggerFactory.getLogger(CustomRealm.class);
   // private final UserMapper userMapper;
/*
    @Autowired
    public CustomRealm(UserMapper userMapper) {
        this.userMapper = userMapper;
    }*/

    /**
     * 必须重写此方法，不然会报错
     */
    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof JWTToken;
    }

    /**
     * 默认使用此方法进行用户名正确与否验证，错误抛出异常即可。
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //System.out.println("————身份认证方法————");
        log.debug("————身份认证方法————");
        String token = (String) authenticationToken.getCredentials();
        // 解密获得userinfo，用于和数据库进行对比
        String userinfo = JWTUtil.getUserInfo(token);
        if (userinfo == null || !JWTUtil.verify(token, userinfo)) {
            throw new AuthenticationException("token认证失败！");
        }
        //String password ="";
        // String password = userMapper.getPassword(username);
        //if (password == null) {
        //    throw new AuthenticationException("该用户不存在！");
        //}
        //int ban = userMapper.checkUserBanStatus(username);
        //if (ban == 1) {
         //   throw new AuthenticationException("该用户已被封号！");
        //}
        return new SimpleAuthenticationInfo(token, token, "MyRealm");
    }

    /**
     * 只有当需要检测用户权限的时候才会调用此方法，例如checkRole,checkPermission之类的
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        log.debug("————权限认证————");
        //String userInfo = JWTUtil.getUserInfo(principals.toString());
        //StaffQueryResponse user = JSON.parseObject(userInfo, StaffQueryResponse.class);
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        //获得该用户角色
        Set<String> roleSet = new HashSet<String>();
        //String roleList = RedisUtil.hget(SmsEnums.RedisKey.RedisKeyCode.SYS_USER_INFO.toString(), "admin");
        //if(!StringUtil.isBlank(roleList)){
        //	JSONArray jsonArray = JSON.parseArray(roleList);
    	//	for(Object o :jsonArray){
    	//		roleSet.add(String.valueOf(o));
    	//	}
        //}
        roleSet.add("admin");
        roleSet.add("user");
		
        //每个角色拥有默认的权限
        //String rolePermission = userMapper.getRolePermission(username);
        //String rolePermission="normal";
        //每个用户可以设置新的权限
        //String permission = userMapper.getPermission(username);
        //Set<String> permissionSet = new HashSet<>();
        //需要将 role, permission 封装到 Set 作为 info.setRoles(), info.setStringPermissions() 的参数
        //permissionSet.add(rolePermission);
        //permissionSet.add(permission);
        //设置该用户拥有的角色和权限
        //info.setStringPermissions(permissionSet);
        info.setRoles(roleSet);
        return info;
    }
}
